package com.zhentao.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhentao.mapper.CdzInviteRecordMapper;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.pojo.income.CdzInviteRecord;
import com.zhentao.model.common.pojo.users.Users;
import com.zhentao.service.ICdzInviteRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.service.IUsersService;
import com.zhentao.userLocal.UserThreadLocalUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mp
 * @since 2024-12-25
 */
@Service
public class CdzInviteRecordServiceImpl extends ServiceImpl<CdzInviteRecordMapper, CdzInviteRecord> implements ICdzInviteRecordService {

    @Resource
    private IUsersService usersService;
    @Override
    public ResponseResult record() {
        // 获取登录人id
        Long userId = UserThreadLocalUtil.getUser();
        // 根据登录人id查询邀请人列表
        LambdaQueryWrapper<CdzInviteRecord> queryWrapper = Wrappers.<CdzInviteRecord>lambdaQuery()
                .eq(CdzInviteRecord::getInviteUserId, userId);
        //遍历
        List<CdzInviteRecord> list = this.list(queryWrapper);
        for (CdzInviteRecord cdzInviteRecord : list) {
            Users user = usersService.getOne(Wrappers.<Users>lambdaQuery().eq(Users::getId, cdzInviteRecord.getInvitedUserId()));
            cdzInviteRecord.setUserName(user.getNickName());
            cdzInviteRecord.setCreateTime(user.getCreateTime());
        }
        if (list.size() == 0) {
            return ResponseResult.errorResult(500, "暂无数据");
        }
        return ResponseResult.okResult(list);
    }

    @Override
    public ResponseResult recordCount() {
        // 获取登录人id
        Long userId = UserThreadLocalUtil.getUser();
        // 根据登录人id查询邀请人列表
        LambdaQueryWrapper<CdzInviteRecord> queryWrapper = Wrappers.<CdzInviteRecord>lambdaQuery()
                .eq(CdzInviteRecord::getInviteUserId, userId);
        // 定义一个常量
        int invitedCount = 0;
        //遍历
        List<CdzInviteRecord> list = this.list(queryWrapper);
        for (CdzInviteRecord cdzInviteRecord : list) {
            invitedCount = invitedCount + 1;
        }
        if (list.size() == 0) {
            return ResponseResult.errorResult(500, "暂无数据");
        }
        return ResponseResult.okResult(invitedCount);
    }

}
